package com.example.dict;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.hardware.Camera;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.TextView;

public class MainActivity extends Activity {

	private CameraPreview mPreview;
	private Bitmap mBitmap;
	private FrameLayout frmLpreview;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		Log.e("here","onCreate");
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

		Button btnRF = (Button) findViewById(R.id.btnRF);
		TextView txtVmean = (TextView) findViewById(R.id.mean);
		final TextView txtVword = (TextView) findViewById(R.id.word);
		frmLpreview = (FrameLayout) findViewById(R.id.preview);

		// Create our Preview view and set it as the content of our activity.
		mPreview = new CameraPreview(this);

		frmLpreview.setOnTouchListener(new OnTouchListener() {
			public boolean onTouch(View v, MotionEvent e) {
				float x = e.getX();
				float y = e.getY();
				Log.e("here", mPreview.getCamera().getParameters().getFocusMode());
				mPreview.setFocus(x, y);
				// TODO Auto-generated method stub
				return false;
			}
		});

		btnRF.setOnClickListener(new OnClickListener() {
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Bitmap src = BitmapFactory.decodeResource(getResources(),
						R.drawable.input);
				Recognition rc = new Recognition(src);
				txtVword.setText(rc.recognizeWord());
				mPreview.takePicture(new Camera.PictureCallback() {

					public void onPictureTaken(byte[] data, Camera camera) {
						// TODO Auto-generated method stub
						try {
							mBitmap = BitmapFactory.decodeByteArray(data, 0,
									data.length);
							String outUriStr = MediaStore.Images.Media
									.insertImage(getContentResolver(), mBitmap,
											"Captured Image",
											"Captured Image using Camera.");
							Log.e("here", outUriStr);
							if (outUriStr == null) {
								Log.e("here", "Image insert failed.");
								return;
							} else {
								Uri outUri = Uri.parse(outUriStr);
								sendBroadcast(new Intent(
										Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,
										outUri));
							}
							mPreview.getCamera().startPreview();
						} catch (Exception e) {
							Log.e("here", "Failed to insert image.", e);
						}
					}
				});
			}
		});

		// StopSensor
		// {
		// mPreview.takePicture(new Camera.PictureCallback() {
		//
		// public void onPictureTaken(byte[] data, Camera camera) {
		// // TODO Auto-generated method stub
		// try{
		// mBitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
		// String outUriStr =
		// MediaStore.Images.Media.insertImage(getContentResolver(), mBitmap,
		// "Captured Image", "Captured Image using Camera.");
		//
		// if(outUriStr==null){
		// Log.e("here","Image insert failed.");
		// return;
		// }else{
		// Uri outUri = Uri.parse(outUriStr);
		// sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,
		// outUri));
		// }
		// mPreview.getCamera().startPreview();
		// }
		// catch(Exception e){
		// Log.e("here","Failed to insert image.",e);
		// }
		// }
		// });}
	}

	@Override
	protected void onPause() {
		Log.e("here","onPause");
		super.onPause();
		frmLpreview.removeAllViews();
		mPreview.releaseCamera();
		// release Camera
	}

	@Override
	protected void onStop(){
		super.onStop();
		Log.e("here","onStop");
	}

	@Override
	protected void onRestart(){
		super.onRestart();
		Log.e("here","onRestart");
	}
	
	@Override
	protected void onStart(){
		super.onStart();
		Log.e("here","onStart");
	}
	
	@Override
	protected void onResume() {
		Log.e("here","onResume");
		super.onResume();
		mPreview.openCamera();
		frmLpreview.addView(mPreview);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

}
